package com.hksj.Intelligentmanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.Intelligentmanagement.common.Result;
import com.hksj.Intelligentmanagement.entity.GasInfoEntity;
import com.hksj.Intelligentmanagement.mapper.GasInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName GasInfoService
 * @Author zengke
 * @Date 2023/7/18 10:00
 */

@Service
public class GasInfoService extends ServiceImpl<GasInfoMapper, GasInfoEntity> {

    //新增气体信息记录
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Result createGasInfo(GasInfoEntity gasInfoEntity){

        //新增气体信息时要进行判断,若是同一栋楼下的同一个科室的气体,则做更新否则做插入
        //创建条件构造器对象
        LambdaQueryWrapper<GasInfoEntity> wrapper = new LambdaQueryWrapper<GasInfoEntity>()
                .eq(GasInfoEntity::getGasId, gasInfoEntity.getGasId())
                .eq(GasInfoEntity::getBuildingId, gasInfoEntity.getBuildingId())
                .eq(GasInfoEntity::getDepartmentId, gasInfoEntity.getDepartmentId());

        //调用baseMapper的selectCount方法,若查询到的结果小于1个做插入否则做更新
        if (this.count(wrapper) < 1){
            //新增操作
            this.save(gasInfoEntity);
            return Result.ok("新增气体信息成功");
        }
        //更新操作
        this.update(gasInfoEntity,wrapper);
        return Result.ok("更新气体信息成功");

    }

}
